<template>
  <div class="app-container">
  
    <!-- 缴费流水查询表单 -->
    <el-form :model="paymentQuery" :inline="true" style="margin-top: 32px">
      <el-form-item label="年月" prop="month">
        <el-date-picker
          v-model="paymentQuery.month"
          type="month"
          format="YYYY-MM"
          value-format="YYYY-MM"
          placeholder="选择年月"
          style="width: 120px"
        />
      </el-form-item>
      <el-form-item label="租户名称" prop="qymc">
        <el-input v-model="paymentQuery.qymc" placeholder="请输入租户名称" style="width: 180px" />
      </el-form-item>
      <el-form-item label="载体" prop="zt">
        <el-select v-model="paymentQuery.zt" placeholder="请选择载体" style="width: 180px">
          <el-option v-for="item in ztOptions" :key="item.nr" :label="item.nr" :value="item.nr" />
        </el-select>
      </el-form-item>
      <el-form-item label="计费类型" prop="jflx">
        <el-select v-model="paymentQuery.jflx" style="width: 120px">
          <el-option label="后付费" :value="1" />
          <el-option label="预充值" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handlePaymentQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetPaymentQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 缴费流水列表 -->
    <el-table
      v-loading="paymentLoading"
      :data="paymentList"
      border
      highlight-current-row
      style="margin-top: 16px"
    >
      <el-table-column prop="zt" label="载体" align="center" />
      <el-table-column prop="qymc" label="租户名称" align="center" />
      <el-table-column prop="czje" label="缴费金额" align="center" />
      <el-table-column prop="sxfje" label="手续费金额" align="center" >
         <template #default="scope">
          {{ scope.row.sxfje ? scope.row.sxfje : 0 }}
        </template>
      </el-table-column>
      <el-table-column label="缴费日期" align="center">
        <template #default="scope">
          {{ scope.row.czrq ? parseTime(scope.row.czrq,"{y}-{m}-{d}") : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="计费类型" align="center">
        <template #default="scope">
          <span v-if="scope.row.jflx === 1">后付费</span>
          <span v-else-if="scope.row.jflx === 2">预充值</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <!-- 备注 -->
      <el-table-column label="备注" align="center">
        <template #default="scope">
          {{ scope.row.remark ? scope.row.remark : '-' }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="paymentTotal > 0"
      :total="paymentTotal"
      v-model:page="paymentQuery.pageNum"
      v-model:limit="paymentQuery.pageSize"
      @pagination="fetchPaymentList"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import {  paymentDetails } from "@/api/bill/index";
import { parseTime } from "@/utils/ruoyi.js";
import { selectzt } from "@/api/carrier/ammeter";



const paymentList = ref([]);
const paymentTotal = ref(0);
const paymentLoading = ref(false);
const ztOptions = ref([]);

function getLastMonthStr() {
  const d = new Date();
  d.setDate(1); // 设置为当月第一天
  d.setMonth(d.getMonth() - 1);

  console.log(parseTime(d, "{y}-{m}"),parseTime(d),d.getMonth());
  return parseTime(d, "{y}-{m}");
}


// 新增缴费流水查询参数
const paymentQuery = reactive({
  pageNum: 1,
  pageSize: 10,
  month: getLastMonthStr(),
  qymc: undefined,
  zt: undefined,
  jflx: 1,
});



function fetchZtOptions() {
  selectzt({  pageNum: 1,
    pageSize: 10,
    nr: undefined,
    qygs: undefined,
    lxbh: 1, // 默认类型编号为1，且不会变动
  }).then(res => {
    ztOptions.value = res.data?.list || [];
    if (ztOptions.value.length > 0 && !paymentQuery.zt) {
      paymentQuery.zt = ztOptions.value[0].nr;
    }
  });
}

function fetchPaymentList() {
  paymentLoading.value = true;
  paymentDetails({ ...paymentQuery }).then(res => {
    paymentList.value = res.data?.list || [];
    paymentTotal.value = res.data?.total || 0;
    paymentLoading.value = false;
  }).catch(() => {
    paymentLoading.value = false;
  });  
}

function handlePaymentQuery() {
  paymentQuery.pageNum = 1;
  fetchPaymentList();
}

function resetPaymentQuery() {
  paymentQuery.month = getLastMonthStr();
  paymentQuery.qymc = undefined;
  paymentQuery.zt = ztOptions.value[0]?.nr;
  paymentQuery.jflx = 1;
  paymentQuery.pageNum = 1;
  fetchPaymentList();
}
``
onMounted(() => {
  fetchZtOptions();
  fetchPaymentList();
});
</script>